package com.zlzxm.shoesmarket.view.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.RelativeLayout;

import androidx.core.content.ContextCompat;

import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.AxisBase;
import com.github.mikephil.charting.components.Description;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.formatter.ValueFormatter;
import com.github.mikephil.charting.interfaces.datasets.ILineDataSet;
import com.zlzxm.shoesmarket.R;
import com.zlzxm.shoesmarket.util.ZViewHelp;
import com.zlzxm.shoesmarket.view.markview.MarkViewPrice;
import com.zlzxm.shoesmarket.viewmodel.entity.EntityChartValue;

import java.util.ArrayList;
import java.util.List;

/**
 * CREATE BY zlzxm ON 4/25/21
 */
public class TendencyView extends RelativeLayout {



    private LineChart lineChart;

    public TendencyView(Context context) {
        this(context,null);
    }

    public TendencyView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public TendencyView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        init();
    }


    private void init(){

        LayoutInflater.from(getContext()).inflate(R.layout.layout_tendency,this,true);

        lineChart = ZViewHelp.findById(this,R.id.lineChart);

        initInOutLineChart();


    }



    private void initInOutLineChart() {

        lineChart.setDragEnabled(false);
        //设置推动
        lineChart.setScaleEnabled(false);

        lineChart.setMarker(new MarkViewPrice(getContext()));

        YAxis leftAxis = lineChart.getAxisLeft();
        YAxis rightAxis = lineChart.getAxisRight();

        leftAxis.setTextColor(ContextCompat.getColor(getContext(),R.color.colorA7A7A7));
        leftAxis.setTextSize(12);

        leftAxis.setAxisMinimum(0);
        rightAxis.setAxisMinimum(0);

        leftAxis.setDrawLabels(true);
        rightAxis.setDrawLabels(false);

        leftAxis.setDrawAxisLine(false);
        rightAxis.setDrawAxisLine(false);

        leftAxis.setAxisMaximum(1000);
        rightAxis.setAxisMaximum(1000);

        leftAxis.setLabelCount(5,true);
        rightAxis.setLabelCount(5,true);


        leftAxis.setGridColor(ContextCompat.getColor(getContext(),
                R.color.colorA8A8A8));
        rightAxis.setGridColor(ContextCompat.getColor(getContext(),
                R.color.colorA8A8A8));

        lineChart.getXAxis().setPosition(XAxis.XAxisPosition.BOTTOM);


        Description description = new Description();
        description.setText("");
        lineChart.setDescription(description);

        lineChart.getXAxis().setDrawGridLines(false);

        Legend legend = lineChart.getLegend();

        legend.setEnabled(false);

        lineChart.setExtraBottomOffset(20);

        LineDataSet lineDataSetIn = new LineDataSet(new ArrayList<>(), "");

        lineDataSetIn.setDrawHorizontalHighlightIndicator(false);
        lineDataSetIn.setDrawVerticalHighlightIndicator(false);

        lineDataSetIn.setColor(ContextCompat
                .getColor(getContext(),R.color.color448DE4));

        lineDataSetIn.setLineWidth(2f);

        lineDataSetIn.setDrawCircles(false);

        lineDataSetIn.setDrawValues(false);

        lineDataSetIn.setMode(LineDataSet.Mode.LINEAR);

        List<ILineDataSet> dataSets = new ArrayList<>();

        dataSets.add(lineDataSetIn);

        LineData lineData = new LineData(dataSets);

        lineChart.setData(lineData);

    }

    public void setChartData(List<EntityChartValue> entityChartValues){

        List<Entry> data  = new ArrayList<>();

        for (int i = 0; i < entityChartValues.size(); i++) {

            data.add(new Entry(i,entityChartValues.get(i).getValue()));

        }

        lineChart.getXAxis().setValueFormatter(new ValueFormatter(){

            @Override
            public String getAxisLabel(float value, AxisBase axis) {


                return entityChartValues.get((int) value).getKey();
            }
        });


        LineData lineData = lineChart.getLineData();

        LineDataSet lineDataSet = (LineDataSet) lineData.getDataSetByIndex(0);

        lineDataSet.setValues(data);

        lineDataSet.notifyDataSetChanged();


        lineData.notifyDataChanged();

        lineChart.notifyDataSetChanged();

        lineChart.invalidate();


    }

}
